package com.example.stretching;


import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import com.google.android.gms.internal.mh;

import android.animation.TimeAnimator;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.sax.StartElementListener;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;

	/**
	 * 일단 broadcastreceiver로 notification을 날려줌.
	 * GCM구현시 서비스에서...
	 * @author DS
	 * 
	 * 
	 */
	
public class AlarmReceive extends BroadcastReceiver implements OnClickListener{
	
	String INTENT_ACTION = Intent.ACTION_BOOT_COMPLETED;
	static long time_alarm;
	final String TAG = "BOOT_START_SERVICE";

		@Override
		public void onReceive(Context context, Intent intent1) {
//			if(GregorianCalendar - 3*60*1000 == System.currentTimeMillis()){
			Log.d("on", "on");
			 boolean[] week_ = intent1.getBooleanArrayExtra("weekday");
			 
//			Notification notif = new Notification();
			NotificationManager nm  = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//			notif.vibrate = new long[] {100, 100, 200, 200, 300, 300, 400, 400, 500, 500};
			
			Calendar cal = Calendar.getInstance();
//			if(cal.get(Calendar.HOUR) == 0){
//				AlarmAdapter.complete = null;
//			}
			Log.d("cal_week", Integer.toString(cal.get(Calendar.DAY_OF_WEEK)));
//			if(week_[MainActivity.gregorianCalendar.get(Calendar.DAY_OF_WEEK)]){
			if(week_[cal.get(Calendar.DAY_OF_WEEK)] == true){
			
				Intent intentActivity = new Intent(context, AnimationPage.class);
				intentActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intentActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP 
						| Intent.FLAG_ACTIVITY_SINGLE_TOP );
			intentActivity.putExtra("hour", Integer.toString(cal.getTime().getHours()));
				
//				Intent i = new Intent();
//		        i.setClassName("com.example", "com.example.AnimationPage");
//		        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//		        context.startActivity(intentActivity);
			PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentActivity, PendingIntent.FLAG_UPDATE_CURRENT);
//			 PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0,  new Intent(mContext, NotiList.class), 0);
			
		
			Calendar calendar = Calendar.getInstance();
			 
			// long으로 가져올 때
			long now = calendar.getTimeInMillis();
			// 문자열로 가져올 때
			int hour_ = calendar.getTime().getHours();
			
			String ticker = "ticker";//여긴 알림바에 등록될 글이랑 타이틀 적는곳. 
			     String title = "title";
			     String text = "알림" ;      
			     
			     String title1 = "title1";
			     String text1 = "제발";
			   // Create Notification Object
			     
			   Notification notification = new Notification
			   (android.R.drawable.ic_input_add, ticker, System.currentTimeMillis());//알림바에 넣을 이미지 아이콘 
				notification.vibrate = new long[] {100, 100, 200, 200, 300, 300, 400, 400, 500, 500};
//				startActivity(intentActivity);
				notification.flags |= Notification.FLAG_AUTO_CANCEL;
				time_alarm = System.currentTimeMillis();
//				try {
//					
//					pendingIntent.send();
//					
//					} catch (CanceledException e) {
//					e.printStackTrace();
//					}
				
			   if(hour_ %2 == 1){
			   notification.setLatestEventInfo(context,   title, text, pendingIntent);
			   nm.notify(0, notification);
			   Log.d("HOUR", Integer.toString((hour_)));
			   }
			   else {
				   Log.d("HOUR2", Integer.toString((hour_)));
			   notification.setLatestEventInfo(context,   title1, text1, pendingIntent);
			   nm.notify(1, notification);
			   }

			
			}
//		}
			
	}


private void startActivity(Intent intentActivity) {
			// TODO Auto-generated method stub
			
		}


@Override
public void onClick(DialogInterface dialog, int which) {
	// TODO Auto-generated method stub

}
		
}